package com.eastfair.messagecenter.duty;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.eastfair.auth.exceptioncode.AuthServiceExceptionCode;
import com.eastfair.core.base.R;
import com.eastfair.core.exception.MessageBizException;
import com.eastfair.core.exception.NoBackBizException;
import com.eastfair.messagecenter.exceptioncode.MessageServiceExceptionCode;
import com.eastfair.messagecenter.pipeline.BusinessProcess;
import com.eastfair.messagecenter.pipeline.ProcessContext;
import com.eastfair.messagecenter.pipeline.SendTaskModel;
import com.eastfair.messagecenter.point.ReadySendPoint;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author ligang
 * @date 2021/11/22
 * @description 前置参数校验
 */
@Slf4j
@Service
public class PreParamCheckAction implements BusinessProcess<SendTaskModel> {

    @Override
    public void process(ProcessContext<SendTaskModel> context) {
        SendTaskModel sendTaskModel = context.getProcessModel();
        ReadySendPoint readySendPoint = sendTaskModel.getReadySendPoint();
        //业务id为空
        if (readySendPoint.getBusinessId() == null) {
            context.setResponse(R.fail(MessageServiceExceptionCode.BUSINESS_CODE_IS_NULL));
            context.setNeedBreak(true);
            throw MessageBizException.wrap("业务id为空");
        }
        //业务类型为空
        if (StringUtils.isBlank(readySendPoint.getBusinessType())) {
            context.setResponse(R.fail(MessageServiceExceptionCode.BUSINESS_CODE_IS_NULL));
            context.setNeedBreak(true);
            throw MessageBizException.wrap("业务类型为空");
        }

        //发送目标为空
        if (CollectionUtil.isEmpty(readySendPoint.getSenUserIdList()) &&
                CollectionUtil.isEmpty(readySendPoint.getOrgIdList()) &&
                (readySendPoint.getIsSendAll() == null || false == readySendPoint.getIsSendAll()) &&
                CollectionUtil.isEmpty(readySendPoint.getSendMobileList())
        ) {
            context.setResponse(R.fail(MessageServiceExceptionCode.SEND_USER_ISNULL));
            context.setNeedBreak(true);
            throw MessageBizException.wrap("发送目标为空");
        }
        //发送渠道为空
        if (readySendPoint.getChannelType() == null) {
            context.setResponse(R.fail(MessageServiceExceptionCode.SEND_CHANNEL_ISNULL));
            context.setNeedBreak(true);
            throw MessageBizException.wrap("发送渠道为空");
        }
        //发送内容为空
        if (CollectionUtil.isEmpty(readySendPoint.getContentModelList())) {
            context.setResponse(R.fail(MessageServiceExceptionCode.SEND_CONTENT_ISNULL));
            context.setNeedBreak(true);
            throw MessageBizException.wrap("发送内容为空");
        }
    }
}
